from sqlalchemy import Column, Integer, String, ForeignKey
from sqlalchemy.orm import relationship
from .base import Base, TimestampMixin

__all__ = [
    'Province', 'Bureau', 'Team', 'Substation'
]


class Province(Base, TimestampMixin):
    __tablename__ = 'cy_province'
    id = Column(Integer, primary_key=True)
    name = Column(String(50), unique=True)
    bureau = relationship("Bureau", back_populates="province")


class Bureau(Base, TimestampMixin):
    __tablename__ = 'cy_bureau'
    id = Column(Integer, primary_key=True)
    name = Column(String(50))
    province_id = Column(Integer, ForeignKey('cy_province.id'))
    province = relationship("Province", back_populates="bureau")

    # Add a ForeignKey to link to 'team'
    team = relationship("Team", back_populates="bureau", foreign_keys="Team.bureau_id")


class Team(Base, TimestampMixin):
    #__tablename__ = 'la_system_auth_post'
    __tablename__ = 'cy_team'
    id = Column(Integer, primary_key=True)
    name = Column(String(50))
    # 定义与 Bureau 的关系
    bureau_id = Column(Integer, ForeignKey('cy_bureau.id'))
    bureau = relationship("Bureau", back_populates="team")
    # 定义与 Substation 的关系
    substation = relationship("Substation", back_populates="team")


class Substation(Base, TimestampMixin):
    __tablename__ = 'cy_substation'
    id = Column(Integer, primary_key=True)
    name = Column(String(50))
    # 添加外键来关联到 Team 表
    team_id = Column(Integer, ForeignKey('cy_team.id'))
    # 定义与 Team 的关系
    team = relationship("Team", back_populates="substation")


province = Province.__table__
bureau = Bureau.__table__
team = Team.__table__
substation = Substation.__table__


#) 创建表
# from sqlalchemy import create_engine
#
# # 配置数据库连接（替换成你的实际数据库连接信息）
# engine = create_engine('mysql+pymysql://root:123456@localhost:3306/cygcadmin?charset=utf8mb4')
#
# # 使用 create_all() 创建所有未创建的表
# Base.metadata.create_all(engine)